package com.mmall.concurrency.example.atomic;

import com.mmall.concurrency.annotions.ThreadSafes;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * <p>Descriptions...
 *
 * @author Diamon.Cheng
 * @date 2019/12/3.
 */
@Slf4j
@ThreadSafes
public class AtomicExample_5 {

    private  static AtomicIntegerFieldUpdater<AtomicExample_5> updater =
            AtomicIntegerFieldUpdater.newUpdater(AtomicExample_5.class,"count");
    
    @Getter
    public  volatile  int count = 100;
   
    
    public static void main(String[] args) {
    
         AtomicExample_5 example_5 = new AtomicExample_5();
         if (updater.compareAndSet(example_5,100,120)){
             log.info("update sucess 1,{}",example_5.getCount());
         }
    
        if (updater.compareAndSet(example_5, 100, 120)) {
            log.info("update success 2, {}", example_5.getCount());
        } else {
            log.info("update failed, {}", example_5.getCount());
        }
         
         
         
    }
    
}
